﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Net;
using System.Text;
using PlugNT.Cms.Page;

namespace PlugNT.Cms.Web.Admin
{
    public partial class _Info :  AdminPage
    {
        protected string MemberName, AdminLevel;
        protected string ServerOS, CpuSum, CpuType, ServerSoft, MachineName, ServerName, ServerPath, ServerNet, ServerArea, ServerTimeOut, ServerStart;
        protected string PrStart, AspNetN, AspNetCpu, ServerSessions, ServerApp, ServerCache, ServerAppN, ServerFso, RunTime, SessionTime, VersionTxt;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (this.adminer != null)
            {
                DataLoad();
            }
        }

        #region 获取服务器及用户信息
        private void DataLoad()
        {
            DateTime sTime = DateTime.Now;

            MemberName = "";

            ServerOS = Environment.OSVersion.ToString();
            CpuSum = Environment.GetEnvironmentVariable("NUMBER_OF_PROCESSORS");
            CpuType = Environment.GetEnvironmentVariable("PROCESSOR_IDENTIFIER");
            ServerSoft = Request.ServerVariables["SERVER_SOFTWARE"];
            MachineName = Server.MachineName;
            ServerName = Request.ServerVariables["SERVER_NAME"];
            ServerPath = Request.ServerVariables["APPL_PHYSICAL_PATH"];
            ServerNet = ".NET CLR " + Environment.Version.ToString();
            ServerArea = (DateTime.Now - DateTime.UtcNow).TotalHours > 0 ? "+" + (DateTime.Now - DateTime.UtcNow).TotalHours.ToString() : (DateTime.Now - DateTime.UtcNow).TotalHours.ToString();
            ServerTimeOut = Server.ScriptTimeout.ToString();
            ServerStart = ((Double)System.Environment.TickCount / 3600000).ToString("N2");
            PrStart = GetPrStart();
            AspNetN = GetAspNetN();
            AspNetCpu = GetAspNetCpu();
            ServerSessions = Session.Contents.Count.ToString();
            ServerApp = Application.Contents.Count.ToString();
            ServerCache = Cache.Count.ToString();
            ServerAppN = GetServerAppN();
            ServerFso = Check("Scripting.FileSystemObject");
            ServerTimeOut = Server.ScriptTimeout.ToString() + "毫秒";
            //新添加
            SessionTime = HttpContext.Current.Session.Timeout.ToString();
            //版本信息
            //string[] versionInfo = System.Reflection.Assembly.GetAssembly(GetType()).FullName.Split(',');
            //VersionTxt = versionInfo[1].Substring(9);
            VersionTxt = PlugNT.Custom.WebConfig.Version;
            this.EnableViewState = false;

            //执行时间
            DateTime eTime = DateTime.Now;
            RunTime = ((eTime - sTime).TotalMilliseconds).ToString();

            //表和字段
            StringBuilder sb = new StringBuilder();
            System.Collections.Generic.Dictionary<string, string[]> tables = PlugNT.Cms.Core.Table.GetShemaTables();
            foreach (System.Collections.Generic.KeyValuePair<string, string[]> keyvalue in tables)
            {
                sb.Append("<tr class=\"tr_southidc\"><td align=\"right\" width=\"150px;\">");
                sb.Append(keyvalue.Key);
                sb.Append("</td><td style=\"word-break:break-all;\">");
                StringBuilder isb = new StringBuilder();
                foreach (string s in keyvalue.Value)
                {
                    isb.Append("　" + s + "　,");
                }
                sb.Append(isb.ToString().Trim(','));
                sb.Append("</td></tr>");
            }
            tables.Clear();
            tables = null;
            GC.Collect();
            litTablePros.Text = sb.ToString();

        }
        #endregion

        #region 获取服务器系统信息
        private string GetServerAppN()
        {
            string temp;
            try
            {
                temp = ((Double)GC.GetTotalMemory(false) / 1048576).ToString("N2") + "M";
            }
            catch
            {
                temp = "未知";
            }
            return temp;
        }

        private string GetAspNetN()
        {
            string temp;
            try
            {
                temp = ((Double)System.Diagnostics.Process.GetCurrentProcess().WorkingSet64 / 1048576).ToString("N2") + "M";
            }
            catch
            {
                temp = "未知";
            }
            return temp;
        }

        private string GetAspNetCpu()
        {
            string temp;
            try
            {
                temp = ((TimeSpan)System.Diagnostics.Process.GetCurrentProcess().TotalProcessorTime).TotalSeconds.ToString("N0");
            }
            catch
            {
                temp = "未知";
            }
            return temp;
        }

        private string GetPrStart()
        {
            string temp;
            try
            {
                temp = System.Diagnostics.Process.GetCurrentProcess().StartTime.ToString();
            }
            catch
            {
                temp = "未知";
            }
            return temp;
        }

        private string Check(string obj)
        {
            try
            {
                object claobj = Server.CreateObject(obj);
                return "支持";
            }
            catch
            {
                return "不支持";
            }
        }
        #endregion
    }
}
